//
//  SignInPopView.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/7.
//

import UIKit
typealias BlockRulesAntion = ()-> Void
class SignInPopView: UIView {
    var bgImageView: UIImageView!
    var titleLab: UILabel!
    var integrationBtn:UIButton!
    var contentLab: UILabel!
    var exchangenBtn: UIButton!
    var hideBtn: UIButton!
    var rulesBtn: UIButton!
    var registerBtn: UIButton!
    var signInModel: SignInDataModel!
//    var shareActivityBlock: BlockShareActivity!
    var rulesAntionBlock: BlockRulesAntion!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let contor = UIControl(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight))
        contor.backgroundColor = .black
        contor.alpha = 0.7
        contor.addTarget(self, action: #selector(close), for: .touchUpInside)
        self.addSubview(contor)
        
        self.bgImageView = UIImageView(frame: CGRect(x: 30, y: (kScreenHeight - 366)/2, width: kScreenWidth - 60, height: 366))
        self.bgImageView.contentMode = .scaleAspectFill
        self.bgImageView.layer.cornerRadius = 8
//        self.bgImageView.image = UIImage(named: "icon_bg_SignIn")
        self.bgImageView.backgroundColor = UIColor.white
        self.bgImageView.clipsToBounds = true
        self.bgImageView.isUserInteractionEnabled = true
        self.addSubview(self.bgImageView)
        
        
        self.titleLab = UILabel(frame: CGRect(x: 20, y: 40, width: self.bgImageView.frame.size.width - 40, height: 20))
        self.titleLab.font = UIFont(name: Theme.PingFangSCRegular, size: 18)
        self.titleLab.textColor = Theme.titleLabColor
        self.titleLab.text = MMLocalizedString("daily_claim_points", comment: "每日领积分")
        self.titleLab.textAlignment = .center
        self.bgImageView.addSubview(self.titleLab)
        
        
        self.integrationBtn = UIButton(frame: CGRect(x: 38, y: self.titleLab.frame.maxY + 12, width: self.bgImageView.frame.size.width - 76, height: 36))
        self.integrationBtn.layer.cornerRadius = 8
        self.integrationBtn.clipsToBounds = true
        self.integrationBtn.backgroundColor = UIColor(hex: 0xFFC422)
        self.integrationBtn.titleLabel?.font = UIFont(name: Theme.PingFangSCRegular, size: 12)
        self.integrationBtn.setTitleColor(Theme.titleLabColor, for: .normal)
        self.integrationBtn.isUserInteractionEnabled = false
        self.integrationBtn.titleLabel?.lineBreakMode = .byWordWrapping
        self.integrationBtn.titleLabel?.numberOfLines = 0
        self.bgImageView.addSubview(self.integrationBtn)
        
        
        self.contentLab = UILabel(frame: CGRect(x: 20, y: self.integrationBtn.frame.maxY + 20, width: self.bgImageView.frame.size.width - 40, height: 100))
        self.contentLab.font = UIFont(name: Theme.PingFangSCMedium, size: 21)
        self.contentLab.textColor = Theme.titleLabColor
        self.contentLab.text = "签到成功获得1000积分"
        self.contentLab.textAlignment = .center
        self.contentLab.numberOfLines = 2
        self.bgImageView.addSubview(self.contentLab)
        
        self.exchangenBtn = UIButton(frame: CGRect(x: 38, y: self.bgImageView.frame.size.height - 105, width: self.bgImageView.frame.size.width - 76, height: 46))
        
        let bgLayer1 = CAGradientLayer()
        bgLayer1.colors = [UIColor(red: 1, green: 0.93, blue: 0.53, alpha: 1).cgColor, UIColor(red: 1, green: 0.71, blue: 0, alpha: 1).cgColor]
        bgLayer1.locations = [0, 1]
        bgLayer1.frame = self.exchangenBtn.bounds
        bgLayer1.startPoint = CGPoint(x: 0.5, y: 0)
        bgLayer1.endPoint = CGPoint(x: 1, y: 1)
        self.exchangenBtn.layer.addSublayer(bgLayer1)
        self.exchangenBtn.setTitleColor(Theme.titleLabColor, for: .normal)
        self.exchangenBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 16)
        self.exchangenBtn.setTitle(MMLocalizedString("points_for_gifts", comment: "积分兑换礼品"), for: .normal)
        self.exchangenBtn.layer.cornerRadius = 8
        self.exchangenBtn.clipsToBounds = true
        self.exchangenBtn.layer.borderColor = Theme.titleLabColor.cgColor
        self.exchangenBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
        self.exchangenBtn.addTarget(self, action: #selector(exchangenBtnAntion), for: .touchUpInside)
        self.bgImageView.addSubview(self.exchangenBtn)
        
        
   
        
        self.rulesBtn = UIButton(frame: CGRect(x: 58, y: self.bgImageView.frame.size.height - 59, width: self.bgImageView.frame.size.width - 116, height: 30))
        self.rulesBtn.setTitleColor(UIColor(hex: 0xFEC52B), for: .normal)
        self.rulesBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        self.rulesBtn.setTitle(MMLocalizedString("activity_rules", comment: "活动规则"), for: .normal)
        self.rulesBtn.addTarget(self, action: #selector(rulesBtnAntion), for: .touchUpInside)
        self.bgImageView.addSubview(self.rulesBtn)

        
        self.hideBtn = UIButton(frame: CGRect(x: (kScreenWidth - 30)/2, y:   self.bgImageView.frame.maxY + 20, width: 30, height: 30))
        self.hideBtn.setImage(UIImage(named: "icon_hide"), for: .normal)
        self.hideBtn.addTarget(self, action: #selector(close), for: .touchUpInside)
        self.addSubview(self.hideBtn)
    }
    
    //setModel
    func setModel(model: SignInDataModel, bkgImg: UIImage? = nil){
        if bkgImg != nil {
            self.bgImageView.image = bkgImg
        }else{
            self.bgImageView.image = UIImage.init(named: "icon_bg_SignIn")
        }
        self.integrationBtn.setTitle(MMLocalizedString("get_random_points", comment: "每日签到可领取随机积分奖励"), for: .normal)
        self.contentLab.text = String(format:  MMLocalizedString("sign_successfully", comment: "签到成功获得%d积分"), model.integral ?? 0)
        
    }
    
    
    
    @objc func close(){
        UIView.animate(withDuration: 0.3, animations: {() -> Void in
            self.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
            self.backgroundColor = UIColor.clear
            self.alpha = 0
        }, completion: {(_ finished: Bool) -> Void in
            self.removeFromSuperview()
        })
    }
    
    @objc func exchangenBtnAntion(){
        //会员奖赏页
        if UserModule.shared.isLogined == false{
           
            let vc = LoginViewController()
            let loginNavi = BaseNavigationController.init(rootViewController: vc)
            loginNavi.modalPresentationStyle = .fullScreen
            self.viewController().present(loginNavi, animated: true)
        }
    }
    
    
    //规则
    @objc func rulesBtnAntion(){
        self.close()
        self.rulesAntionBlock?()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
